package cn.zly.spring.beans.factory.support;

import cn.zly.spring.beans.factory.BeansException;
import cn.zly.spring.beans.factory.ConfigurableListableBeanFactory;
import cn.zly.spring.beans.factory.config.BeanDefinition;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author zhanglianyong
 * @date 2024/12/30
 */
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements BeanDefinitionRegistry, ConfigurableListableBeanFactory {

	private Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>();


	@Override
	public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
		this.beanDefinitionMap.put(beanName, beanDefinition);
	}

	@Override
	public boolean containBeanDefinition(String beanName) {
		return this.beanDefinitionMap.containsKey(beanName);
	}


	@Override
	public BeanDefinition getBeanDefinition(String beanName) {
		BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
		if (beanDefinition == null) {
			throw new BeansException("No bean named '" + beanName + "' is defined");
		}
		return beanDefinition;
	}

	@Override
	public void preInstantiateSingletons() throws BeansException {
		beanDefinitionMap.keySet().forEach(this::getBean);
	}

	@Override
	public <T> Map<String, T> getBeansOfType(Class<T> requireType) {
		return Collections.emptyMap();
	}
}
